<?php

namespace addons\shopro\controller;

use app\admin\model\AdminLog;
use app\admin\model\CatOrder;
use app\api\controller\Login;
use baofu\Baofu;
use think\Db;
use app\common\library\Sms;
use fast\Random;
use think\Validate;
use addons\shopro\library\Wechat;
use addons\shopro\model\UserOauth;
use addons\shopro\model\User as UserModel;
use addons\shopro\model\UserStore;
use addons\shopro\model\Config;
use think\Loader;

/**
 * 会员接口
 */
class Demo extends Base
{
    protected $noNeedLogin = ['userData'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        return parent::_initialize();
    }

    /**
     * 获取用户数据
     *
     * @return void
     */
    public function userData()
    {

        //直接登录会员
        $this->auth->direct(9);
        $user = $this->auth->getUserinfo();
        $this->success('用户数据', $user);
    }

    public function aaa(){

        $bank_card_code = $this->request->param('card_code'); //银行卡号
        $cardholder_name = $this->request->param('cardholder_name', ''); //持卡人名称
        $id_number = $this->request->param('id_number', ''); //证件号， 身份证号
        $phone = $this->request->param('phone'); //手机号码
        $bank_card_security_code = $this->request->param('bank_card_security_code', ''); //银行卡安全码
        $bank_card_validity_period = $this->request->param('bank_card_validity_period', ''); //银行卡有效期

        //账户信息  [银行卡号|持卡人姓名|证件号|手机号|银行卡安全码|银行卡有效期]
        $Cardinfo = "{$bank_card_code}|{$cardholder_name}|{$id_number}|{$phone}|{$bank_card_security_code}|{$bank_card_validity_period}";

        $config = Db::name('config')->where('group', 'baofu')->column('value', 'name');
        Loader::import("\baofu\baofu", EXTEND_PATH);
        $this->baofu = new \baofu\Baofu($config);

        $baofuData = [
            "user_id" => 0,
            'msg_id' => $this->baofu->getMsgID()
        ];





        $result = $this->baofu->queryBind('6217580100006732986','9');
        //$result = $this->baofu->ABolishBind($ProtocolNo = '1202312281445362012408233589',"9");


        $data['order'] = Db::name('shopro_cats_order')->where('id',921)->find();
        $data = $data['order'];

        \think\Queue::later(5, '\addons\shopro\job\OrderAutoOper@baofuPay', ['order' => $data], 'shopro');
        die;
    }

    public function queryPay(){
        $data['baofuData'] = Db::name('baofu_pay')->where('id',283)->find();
        $payData = $data['baofuData'];
        $orderInfo = CatOrder::get($payData['order_id']);

        Loader::import("\baofu\pay",EXTEND_PATH);
        $config = Db::name('config')->where('group','baofu')->column('value','name');
        $baofuClass = new \baofu\Pay($config);

        $result = $baofuClass->queryPay(['trans_id' => $payData['trans_id']]);

        if($result['code'] == 200){
            //支付成功
            //剩余周期减一 remaining_cycle
            CatOrder::where('order_sn', $orderInfo['order_sn'])->setDec('remaining_cycle', 1);
            CatOrder::where('order_sn', $orderInfo['order_sn'])->update(['status' => 4, 'paytime' => time(),'repayment'=>date('d')]);

            //检查当前剩余扣款周期
            $info = CatOrder::where('order_sn', $orderInfo['order_sn'])->value('remaining_cycle');
            if($info != 0){
                //获取下个月的时间
                $time = getNextMonthDays($orderInfo);
                $time = time() + 30;
                \think\Queue::later($time - time(), '\addons\shopro\job\OrderAutoOper@baofuPay', ['order' => $orderInfo], 'shopro');
            }

            $status = 1;
        }elseif($result['code'] == 201){
            //处理中，继续查询
            \think\Queue::later(10, '\addons\shopro\job\OrderAutoOper@baofuQueryPay', ['baofuData' => $payData], 'shopro');
            $status = 3;
        }else{
            //支付失败
            $time = time() + 30;
            \think\Queue::later($time - time(), '\addons\shopro\job\OrderAutoOper@baofuPay', ['order' => $orderInfo], 'shopro');
            $status = 2;
        }

        $notify = json_encode($result['notify'],JSON_UNESCAPED_UNICODE );
        Db::name('baofu_pay')->where(['trans_id' => $payData['trans_id']])->update(['status' => $status,'notify2' => $notify]);

        die;
    }

}
